<?php
/**
 * Created by PhpStorm.
 * User: yangfan
 * Date: 2020/1/15
 * Time: 4:04 PM
 */

namespace App\Utils;

use Illuminate\Support\Facades\Redis;

/**
 * 模拟第三方流水号
 * Class LjsjMockTradeNoGeneratorUtil
 * @package App\Utils
 */
class LjshMockTradeNoGeneratorUtil
{
    /**
     * 模拟第三方交易流水号
     */
    public const MOCK_OUT_TRADE_NO_PREFIX = '700';
    public const MOCK_OUT_TRADE_NO_IDENTIFY = 'MOCK_OUT_TRADE_NO_IDENTIFY:';

    /**
     * 模拟第三方退款流水号
     */
    public const MOCK_OUT_REFUND_TRADE_NO_PREFIX = '800';
    public const MOCK_OUT_REFUND_TRADE_NO_IDENTIFY = 'MOCK_OUT_REFUND_TRADE_NO_IDENTIFY:';

    /**
     * @return string
     */
    public static function getMockOutTradeNo(): string
    {
        $timestamp = time();
        $key = self::MOCK_OUT_TRADE_NO_IDENTIFY . $timestamp;

        $nu = Redis::incr($key);
        Redis::expire($key, 5);

        return (string) self::MOCK_OUT_TRADE_NO_PREFIX . Date('YmdHis', $timestamp) . $timestamp . sprintf('%06s', $nu);
    }

    /**
     * @return string
     */
    public static function getMockRefundOutTradeNo(): string
    {
        $timestamp = time();
        $key = self::MOCK_OUT_REFUND_TRADE_NO_IDENTIFY . $timestamp;

        $nu = Redis::incr($key);
        Redis::expire($key, 5);

        return (string) self::MOCK_OUT_REFUND_TRADE_NO_PREFIX . Date('YmdHis', $timestamp) . $timestamp . sprintf('%06s', $nu);
    }
}
